require('./index.css');
require('page/common/top/index.js');
var _util = require('util/util.js');
var _catalog_service = require('service/catalog-service.js');
var _account_service = require('service/account-service.js');
var listTemplate = require('./index.string');

var catalogList = {
    listDate : {
        title       : '',
        headList    : [],
        productList : {},
        itemList    : {}
    },
    productHeadList:[
        'Product ID',
        'Name'
    ],
    itemHeadList:[
        'Item ID',
        'Product ID',
        'Description',
        'List Price',
        ''
    ],
    init : function(){
        // this.bindEvents();
        this.loadAccountInfo();
        return this;
    },
    bindEvents : function(){
        $('.add_Btn').on("click", function () {
            var _this = this
            var isLogin = false;
            _account_service.checkLogin(
                function (res) {
                    var itemId = $(_this).parent().parent().children('td').first().children().first().text();
                    _catalog_service.addToCart(
                        itemId,
                        function (res) {
                            window.location.href = './cart.html';
                        },
                        function (err) {
                            console.log("ERROR...")
                        }
                    )
                },
                function (err) {
                    alert("请先登录")
                    window.location.href = './user-login.html'
                }
            )
        })
    },
    loadAccountInfo : function(){   
        var id = _util.getURLParam('id');
        var route = _util.getURLParam('route');
        var _this = this;

        if(route === 'product'){
            _catalog_service.getProductList(id,
                function(res){
                    //添加头部信息
                    _this.listDate.headList = _this.productHeadList
                    _this.listDate.title = id;
                    _this.listDate.productList = res;
                    _this.listDate.itemList = null;
                    //渲染数据
                    var result = _util.renderHtml(listTemplate,{ListDate : _this.listDate})
                    console.log(result);
                    $('#Catalog').html(result);
                    _this.bindEvents();
                },
                function(){
                    
                }    
            );
        }
        if(route === 'item'){
            _catalog_service.getItemList(id,
                function(res){
                    //添加头部信息
                    _this.listDate.headList = _this.itemHeadList
                    _this.listDate.title = id;
                    _this.listDate.productList = null;
                    _this.listDate.itemList = res;
                    //渲染数据
                    var result = _util.renderHtml(listTemplate,{ListDate : _this.listDate})
                    console.log(result);
                    $('#Catalog').html(result);
                
                    _this.bindEvents();
                },
                function(){
                    
                }    
            );
        }

        if(route === 'Search'){
            _catalog_service.SearchProductList(id,
                function(res){
                    //添加头部信息
                    _this.listDate.headList = _this.productHeadList
                    _this.listDate.title = '搜索 '+id+' 的结果';
                    _this.listDate.productList = res;
                    _this.listDate.itemList = null;
                    //渲染数据
                    var result = _util.renderHtml(listTemplate,{ListDate : _this.listDate})
                    console.log(result);
                    $('#Catalog').html(result);
                    _this.bindEvents();
                },
                function(){
                    
                }    
            );
        }

    },
};


module.exports = catalogList.init();